package fnord.jovian.tango;

import java.awt.*;
import java.awt.geom.*;
import java.util.*;

import fnord.jovian.TheBoard;

public class ThePanel implements TheObject {

	protected RectangularShape _shape;
	protected Vector<TheObject> _objects;
	protected Vector<TheOrientation> _orientations;
	protected TheLayout _layout = new TheAssemblyLayout();
	protected Color _background = Color.BLACK;
	protected float _alpha = 0.3f;
	protected boolean _showBackground = true;

	protected TheBoard _board;
	
	public ThePanel() {
		_objects = new Vector<TheObject>();
		_orientations = new Vector<TheOrientation>();
	}
	
	public ThePanel(double x, double y, double w, double h, TheBoard board) {
		_board = board;
		_shape = new RoundRectangle2D.Double(x,y,w,h,20,20);
		_objects = new Vector<TheObject>();
		_orientations = new Vector<TheOrientation>();
	}
	

	
	@Override
	public void paint(Graphics2D g) {
	    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
	    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha));
	    g.setColor(_background);
	    if (_showBackground)
	    	g.fill(_shape);
	    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f));
	    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);
		_layout.layout(_objects, _orientations, _shape.getX(), _shape.getY(), _shape.getWidth(), _shape.getHeight());
		for (int i = 0; i < _objects.size(); i++) {
			_objects.get(i).paint(g);
		}
	}
	
	public void showBackground(boolean show) {
		_showBackground = show;
	}
	
	public boolean showBackground() {
		return _showBackground;
	}

	public void setTheLayout(TheLayout layout) {
		_layout = layout;
	}
	
	public void addComponent(TheObject o) {
		_objects.add(o);
	}

	public void addComponent(TheObject o, TheOrientation or) {
		_objects.add(o);
		_orientations.add(or);
	}

	public boolean contains(Point p) {
		boolean contains = false;
		for (int i = 0; i < _objects.size(); i++) {
			if (_objects.get(i).contains(p))
				contains = true;
		}
		return contains;
	}

	public boolean containsClick(Point p) {
		boolean contains = false;
		for (int i = 0; i < _objects.size(); i++) {
			if (_objects.get(i).containsClick(p))
				contains = true;
		}
		return contains;
	}

	public boolean containsUp(Point p) {
		boolean contains = false;
		for (int i = 0; i < _objects.size(); i++) {
			if (_objects.get(i).containsUp(p))
				contains = true;
		}
		return contains;
	}

    public boolean containsDrag(Point p) {
		boolean contains = false;
		for (int i = 0; i < _objects.size(); i++) {
			if (_objects.get(i).containsDrag(p))
				contains = true;
		}
		return contains;
    }

	public double getHeight() {
		return _shape.getHeight();
	}

	public double getWidth() {
		return _shape.getWidth();
	}

	public double getX() {
		return _shape.getX();
	}

	public double getY() {
		return _shape.getY();
	}

	public void setLocation(double x, double y) {
		double dX = x - _shape.getX();
		double dY = y - _shape.getY();
		_shape.setFrame(x, y, _shape.getWidth(), _shape.getHeight());
		for (int i = 0; i < _objects.size(); i++) {
			TheObject o = _objects.get(i);
			o.setLocation(o.getX() + dX, o.getY() + dY);
		}
	}

	public void setSize(double w, double h) {
		_shape.setFrame(_shape.getX(), _shape.getY(), w, h);
	}

    public double getCenterX() {
	    return _shape.getCenterX();
    }

    public double getCenterY() {
	    return _shape.getCenterY();
    }

}
